package com.xxl.sso.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxl.sso.server.core.model.UserInfo;
import com.xxl.sso.server.core.result.ReturnT;
import com.xxl.sso.server.mapper.UserMapper;
import com.xxl.sso.server.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户服务impl
 *
 * @author cong.zhen
 * @date 2023/01/19
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public ReturnT<UserInfo> findUser(String username, String password) {

        if (username==null || username.trim().length()==0) {
            return new ReturnT<UserInfo>(ReturnT.FAIL_CODE, "Please input username.");
        }
        if (password==null || password.trim().length()==0) {
            return new ReturnT<UserInfo>(ReturnT.FAIL_CODE, "Please input password.");
        }
        UserInfo userInfo = userMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUserName, username));
        if(ObjectUtils.isEmpty(userInfo)){
            return new ReturnT<UserInfo>(ReturnT.FAIL_CODE, "username does not exist");
        }

        if(!password.equals(userInfo.getPassword())){
            return new ReturnT<UserInfo>(ReturnT.FAIL_CODE, "username or password is invalid.");
        }
        return new ReturnT<UserInfo>(userInfo);
    }

    @Override
    public ReturnT<Boolean> signUser(UserInfo userInfo) {
       int i = userMapper.insert(userInfo);
       return  new ReturnT<Boolean>(i>0);
    }


}
